/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myjme.appstate;

import Globals.AllGlobal;
import brain.MyBrain;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import com.jme3.texture.Texture;
import controller.MyController;

/**
 *
 * @author Administrator
 */
public class GongZuoMianAppState extends AbstractAppState{

    private float gzmcdF=0;
    private float gzmzxcdF=0;
    private String mingcheng=java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("某工作面");
    private SimpleApplication app;
    private Camera cam;
    private Node rootNode;
    private AssetManager assetManager;
    private Ray ray = new Ray();
    private static Box mesh = new Box(Vector3f.ZERO, 1, 1, 1);
    private MyBrain brain;
    private MyController controller;
    private Node gzmNode=new Node(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("GZM"));
    private Node zhijiaNode=new Node(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("ZHIJIA"));
   
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        this.app          = (SimpleApplication) app;
        this.cam          = this.app.getCamera();
        this.rootNode     = this.app.getRootNode();
        this.assetManager = this.app.getAssetManager();      
        rootNode.attachChild(gzmNode);
        rootNode.attachChild(zhijiaNode);
        
    }

    @Override
    public void update(float tpf) {
        super.update(tpf); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void cleanup() {
        super.cleanup(); 
        while(gzmNode.getChildren().size()>0){
            gzmNode.getChildren().remove(gzmNode.getChildren().size()-1);
        }
        while(zhijiaNode.getChildren().size()>0){
            zhijiaNode.getChildren().remove(zhijiaNode.getChildren().size()-1);
        }
        //gzmNode.rotate(-controller.getMEICENGQINGJIAOF()*FastMath.DEG_TO_RAD, 0, 0);
    }
    public void drawHangDao(float gzmcdF,float gzmzxcdF,String mingcheng,int drawType){
        //gzmNode.rotate(controller.getMEICENGQINGJIAOF()*FastMath.DEG_TO_RAD, 0, 0);
        
        gzmNode.setLocalRotation(new Quaternion().fromAngleAxis(controller.getMEICENGQINGJIAOF()*FastMath.DEG_TO_RAD, Vector3f.UNIT_X));
        
        
        while(gzmNode.getChildren().size()>0){
            gzmNode.getChildren().remove(gzmNode.getChildren().size()-1);
        }
        while(zhijiaNode.getChildren().size()>0){
            zhijiaNode.getChildren().remove(zhijiaNode.getChildren().size()-1);
        }
        if(controller.getGZM_TYPE()==AllGlobal.GZM_TYPE_LEFT){
            if(gzmcdF>0&&gzmzxcdF>0){
                System.out.println(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("GZMAPPSTATE1"));
                while(gzmNode.getChildren().size()>0){
                    gzmNode.getChildren().remove(gzmNode.getChildren().size()-1);
                }
                while(zhijiaNode.getChildren().size()>0){
                    zhijiaNode.getChildren().remove(zhijiaNode.getChildren().size()-1);
                }
                this.gzmcdF=gzmcdF;
                this.gzmzxcdF=gzmzxcdF;
                this.mingcheng=mingcheng;
                
                Box MEICENG=new Box(new Vector3f(gzmcdF/2.0f,0f,-gzmzxcdF/2.0f), gzmcdF/2.0f+2f, 1f, gzmzxcdF/2.0f+1);
                Geometry MEICENGGEO=new Geometry(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("SHANG1"), MEICENG);
                Material material1=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/MISC/UNSHADED.J3MD"));
                material1.setColor("Color", ColorRGBA.Black);
                MEICENGGEO.setMaterial(material1);
                gzmNode.attachChild(MEICENGGEO);
//                Material material=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/LIGHT/LIGHTING.J3MD"));
//                material.setTexture(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DIFFUSEMAP"), assetManager.loadTexture(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("TEXTURES/MEI.JPG")));
//                material.getTextureParam(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DIFFUSEMAP")).getTextureValue().setWrap(Texture.WrapMode.Repeat);
//                material.setBoolean(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("USEMATERIALCOLORS"), true);
//                material.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("AMBIENT"), ColorRGBA.Gray);
//                material.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DIFFUSE"), ColorRGBA.Blue);
//                material.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("SPECULAR"), ColorRGBA.Pink);
//                for(int i=0;i<=(int)(gzmzxcdF/4);i++){
//                    Spatial spatial=makeCave(0,0,-i);
//                    spatial.setMaterial(material);
//                    gzmNode.attachChild(spatial);
//                }
//                for(int i=0;i<=(int)(gzmzxcdF/4);i++){
//                    Spatial spatial=makeCave(gzmcdF,0,-i);
//                    spatial.setMaterial(material);
//                    gzmNode.attachChild(spatial);
//                }
//                //注意这是水瓶工作面啊，不要忘了
//                for(int i=0;i<=(int)(gzmcdF/2-2);i++){
//                    Spatial spatial=makemeibi(3.5f+i*2+0.2f,0,1.5f);
//                    spatial.setMaterial(material);
//                    spatial.rotate(0, -90* FastMath.DEG_TO_RAD, 0);
//                    zhijiaNode.attachChild(spatial);
//                    Spatial yeyazhijia=makeYeYaZhiJia(3.5f+i*2+0.2f,0,1.5f);
//                    spatial.setMaterial(material);
//                    yeyazhijia.rotate(0, 90* FastMath.DEG_TO_RAD, 0);
//                    /////zhijiaNode.attachChild(yeyazhijia);
//                    
//                }            
            }
        }else if(controller.getGZM_TYPE()==AllGlobal.GZM_TYPE_RIGHT){
            if(gzmcdF>0&&gzmzxcdF>0){
                System.out.println(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("GZMAPPSTATE2"));
                while(gzmNode.getChildren().size()>0){
                    gzmNode.getChildren().remove(gzmNode.getChildren().size()-1);
                }
                while(zhijiaNode.getChildren().size()>0){
                    zhijiaNode.getChildren().remove(zhijiaNode.getChildren().size()-1);
                }
                this.gzmcdF=gzmcdF;
                this.gzmzxcdF=gzmzxcdF;
                this.mingcheng=mingcheng;
//                Material material=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/LIGHT/LIGHTING.J3MD"));
//                material.setTexture(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DIFFUSEMAP"), assetManager.loadTexture(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("TEXTURES/MEI.JPG")));
//                material.getTextureParam(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DIFFUSEMAP")).getTextureValue().setWrap(Texture.WrapMode.Repeat);
//                material.setBoolean(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("USEMATERIALCOLORS"), true);
//                material.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("AMBIENT"), ColorRGBA.Gray);
//                material.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DIFFUSE"), ColorRGBA.Blue);
//                material.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("SPECULAR"), ColorRGBA.Pink);
//                for(int i=0;i<=(int)(gzmzxcdF/4);i++){
//                    Spatial spatial=makeCave(0,0,-i);
//                    spatial.setMaterial(material);
//                    gzmNode.attachChild(spatial);
//                }
//                for(int i=0;i<=(int)(gzmzxcdF/4);i++){
//                    Spatial spatial=makeCave(gzmcdF,0,-i);
//                    spatial.setMaterial(material);
//                    gzmNode.attachChild(spatial);
//                }
//                //注意这是水瓶工作面啊，不要忘了
//                for(int i=0;i<=(int)(gzmcdF/2-2);i++){
//                    Spatial spatial=makemeibi(3.5f+i*2+0.2f,0,-gzmzxcdF);
//                    spatial.setMaterial(material);
//                    spatial.rotate(0, 90* FastMath.DEG_TO_RAD, 0);
//                    zhijiaNode.attachChild(spatial);
//                    Spatial yeyazhijia=makeYeYaZhiJia(3.5f+i*2+0.2f,0,-gzmzxcdF);        
//                    spatial.setMaterial(material);
//                    yeyazhijia.rotate(0, 90* FastMath.DEG_TO_RAD, 0);
//                    //////zhijiaNode.attachChild(yeyazhijia);
//                    spatial.rotate(0, 180*FastMath.DEG_TO_RAD, 0);
//                    yeyazhijia.rotate(0, 180*FastMath.DEG_TO_RAD, 0);
//                }
            }
        }             
    }
    //这个直接从数据库里面读取数据进行巷道的绘制
    public void readData(){
        
    }
     /**
     *下面是调用模型库里的一段巷道，进行组装
     */
    public Spatial makeCave(float x,float y,float z){
        Spatial cave=assetManager.loadModel(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("MODELS/HANGDAO_CFT/HANGDAO_CHANGFANGXING.J3O"));
        cave.rotate(0, 90*FastMath.DEG_TO_RAD, 0);
        cave.setLocalTranslation(x, 0f, z*4);
        return cave;
    }
    public Spatial makeYeYaZhiJia(float x,float y,float z){
        Spatial zhijia=assetManager.loadModel(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("MODELS/YAYAZHIJIA/YEYAZHIJIA.J3O"));
        zhijia.setLocalTranslation(x, -1f, z*2+1);
        zhijia.move(0f, 0.4f, 2.6f);
        zhijia.scale(1, 0.75f, 1);
        return zhijia;
    }
    public Spatial makemeibi(float x,float y,float z){
        Spatial meibi=assetManager.loadModel(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("MODELS/MEIBI/MEIBI.J3O"));
        meibi.setLocalTranslation(x, 0f, z*2);
        meibi.move(-1.3f, -1.6f, 3f);        
        
        return meibi;
        
    }
    
    public void addBrain(MyBrain brain){
        this.brain=brain;
    }
    public void addController(MyController controller){
        this.controller=controller;
    }
}
